import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:starfish_tenement_app/pages/house_resource/detail/house_resource_detail_page.dart';
import '../../api/models/home_banner_data.dart';
import '../../api/models/house_res_data.dart';
import '../../component/app_bar/app_search_bar.dart';
import '../../component/banner/home_banner_widget.dart';
import '../../component/filter/filter_menu_widget.dart';
import '../../component/house_list/house_res_list_item.dart';
import '../../component/sliver/sliver_header.dart';
import '../../component/title/big_title.dart';
import '../../route/route_path.dart';
import '../../route/route_utils.dart';
import 'house_resource_vm.dart';

///房源列表页面
class HouseResourcePage extends StatefulWidget {
  const HouseResourcePage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _HouseResourcePageState();
  }
}

class _HouseResourcePageState extends State<HouseResourcePage> {
  final HouseResourceVM _resourceVm = HouseResourceVM();

  @override
  void initState() {
    super.initState();
    _resourceVm.getHomeData();
    _resourceVm.getHouseRes();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<HouseResourceVM>(
        create: (context) {
          return _resourceVm;
        },
        child: Scaffold(
            backgroundColor: Colors.white,
            body: SafeArea(
                child: RefreshIndicator(
              onRefresh: _resourceVm.handleRefresh,
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 14.w),
                // padding: EdgeInsets.only(left: 16.w, right: 15.w),
                child: CustomScrollView(
                  slivers: [
                    SliverToBoxAdapter(
                        child: Column(children: [
                      //搜索栏
                      _titleBar(),
                      SizedBox(height: 22.h),
                      _banner(),
                    ])),

                    SliverHeader(children: [
                      BigTitle(bigTitle: "新房源"),
                      _filterArea(),
                    ]),

                    //房源列表
                    _houseResListView()
                  ],
                ),
              ),
            ))));
  }

  Widget _titleBar() {
    return Container(
        padding: EdgeInsets.only(top: 18.h),
        width: double.infinity,
        child: Row(children: [
          Expanded(
            child: AppSearchBar(
              hintText: "寻找附近房源",
              margin: EdgeInsets.zero,
              rightIconPadding: EdgeInsets.only(left: 25.w, right: 20.w),
              searchType: SearchType.circle,
              // 左侧地理位置按钮
              showLeftMenu: true,
              leftMenu: Text("北京"),
              // 右侧消息按钮
              showRightMenu: true,
              rightMenu: GestureDetector(
                child: Image.asset("assets/images/icon_message.png",
                    width: 18.r, height: 18.r),
                onTap: () {
                  //点击消息按钮
                  RouteUtils.pushForNamed(context, RoutePath.conversationPage);
                },
              ),
            ),
          )
        ]));
  }

  ///banner轮播
  Widget _banner() {
    return Selector<HouseResourceVM, List<HomeBannerData>?>(
        builder: (context, List<HomeBannerData>? bList, child) {
      return BannerWidget(
          dotType: BannerDotType.square,
          bannerData: _resourceVm.generalBannerList(bList),
          bannerClick: (index) {
            //banner点击
          });
    }, selector: (context, vm) {
      return vm.banner;
    });
  }

  ///筛选条件区域
  Widget _filterArea() {
    return Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
      FilterMenuWidget(name: "区域", selected: true),
      FilterMenuWidget(name: "租金"),
      FilterMenuWidget(name: "户型"),
      FilterMenuWidget(name: "筛选")
    ]);
  }

  ///房源列表
  Widget _houseResListView() {
    return Selector<HouseResourceVM, List<HouseResData>?>(
        builder: (context, houseResList, child) {
      return SliverList(
          delegate: SliverChildBuilderDelegate((context, index) {
        HouseResData? item = houseResList?[index];
        return HouseListItem(
          data: item,
          onTap: () {
            RouteUtils.push(context, HouseResourceDetailPage(id: item?.id));
          },
        );
      }, childCount: houseResList?.length ?? 0));
    }, selector: (context, vm) {
      return vm.houseResList;
    });
  }
}
